package com.study.utils;

import org.junit.jupiter.api.Test;
import org.threeten.extra.*;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

/**
 * @author zhuzi
 * 2024/10/28 20:4
 * https://segmentfault.com/a/1190000010892815
 * https://mp.weixin.qq.com/s/42GJ6zEkG3uPSeMy4UJPpw
 */
public class ThreetenDemo {
    LocalDateTime first = LocalDateTime.parse("2017-07-03T10:15:30", DateTimeFormatter.ISO_DATE_TIME);
    LocalDateTime second = LocalDateTime.parse("2011-08-08T12:15:10", DateTimeFormatter.ISO_DATE_TIME);

    @Test
    public void testYears() {
        Years between = Years.between(first, second);
        System.out.println(between.abs());
    }

    @Test
    public void testMonths() {
        Months between = Months.between(first, second);
        System.out.println(between.get(ChronoUnit.MONTHS));
    }

    @Test
    public void testWeeks() {
        System.out.println(Weeks.between(first, second));
    }

    @Test
    public void testDays() {
        Days between = Days.between(LocalDate.parse("2017-08-01"), LocalDate.parse("2017-08-03"));
        System.out.println(between.get(ChronoUnit.DAYS));
    }

    @Test
    public void testHours() {
        System.out.println(Hours.between(first, second).getAmount());
        LocalDateTime now = LocalDateTime.now();
        Hours between = Hours.between(now, now.plusHours(5));
        long l = between.get(ChronoUnit.HOURS);
        System.out.println(l);
        System.out.println(between.getAmount());
    }

    @Test
    public void testMinutes() {
        System.out.println(Minutes.between(first, second));
    }

    @Test
    public void testSeconds() {
        System.out.println(Seconds.between(first, second));
    }

    @Test
    public void testAmPm() {
        System.out.println(AmPm.from(LocalTime.now()));
    }

    @Test
    public void testDayOfMonth() {
        System.out.println(DayOfMonth.from(LocalDate.now()));
    }

    @Test
    public void testDayOfYear() {
        DayOfYear dayOfYear = DayOfYear.from(LocalDate.now());
        System.out.println(dayOfYear.toString());
        System.out.println(dayOfYear.getValue());
    }

    @Test
    public void testDuration() {
        PeriodDuration between = PeriodDuration.between(first, second);
        System.out.println(between.getPeriod());
        System.out.println(between.getDuration());
        System.out.println(between.toString());
    }

    @Test
    public void testDuration2() {
        Instant now = Instant.now();
        Interval interval1 = Interval.of(now, now.plus(Duration.ofDays(5)));
        Interval interval2 = Interval.of(now.plus(Duration.ofDays(3)), now.plus(Duration.ofDays(8)));

        //Interval intersection = Intervals.intersectionOf(interval1, interval2);
        //System.out.println("Intersection: " + intersection);

        //List<Interval> union = Intervals.unionOf(interval1, interval2);
        //System.out.println("Union: " + union);
    }
}
